/**
 * Created by IAN on 2016/10/14.
 */
$(function () {
    //加载部门
    var departSel = $.Params.create();
    departSel.addParams("parentId", "1");
    departSel.ajax(getIP() + "department/getDepartmentPage", function (data) {
        if (data.success) {
            var departFirstHtml = "<option value = 'qita'>一级部门</option>";
            if (data.total > 0) {
                $.each(data.result, function (i, val) {
                    departFirstHtml += "<option value = '" + val.id + "'>" + val.name + "</option>";
                })

            }
            $("#depart1").html(departFirstHtml);
        }
    })

    $("#depart1").on("change", function () {
        var departId = $(this).val();
        if (departId == "qita") {
            var departSecondHtml = "<option value = 'qita'>二级部门</option>";
            $("#depart2").html(departSecondHtml);
            var departThirdHtml = "<option value = 'qita'>三级部门</option>";
            $("#depart3").html(departThirdHtml);
        } else {
            var departSecondSel = $.Params.create();
            departSecondSel.addParams("parentId", departId);
            departSecondSel.ajax(getIP() + "department/getDepartmentPage", function (data) {
                if (data.success) {
                    var departSecondHtml = "<option value = 'qita'>二级部门</option>";
                    if (data.total > 0) {
                        $.each(data.result, function (i, val) {
                            departSecondHtml += "<option value = '" + val.id + "'>" + val.name + "</option>";
                        })

                    } else {
                        departSecondHtml = "<option value = 'qita'>二级部门</option>";
                    }
                    $("#depart2").html(departSecondHtml);
                    var departThirdHtml = "<option value = 'qita'>三级部门</option>";
                    $("#depart3").html(departThirdHtml);
                }
            })
        }
    })

    $("#depart2").on("change", function () {
        var departId = $(this).val();
        if (departId == "qita") {
            var departThirdHtml = "<option value = 'qita'>三级部门</option>";
            $("#depart3").html(departThirdHtml);
        } else {
            var departThirdSel = $.Params.create();
            departThirdSel.addParams("parentId", departId);
            departThirdSel.ajax(getIP() + "department/getDepartmentPage", function (data) {
                if (data.success) {
                    var departThirdHtml = "<option value = 'qita'>三级部门</option>";
                    if (data.total > 0) {
                        $.each(data.result, function (i, val) {
                            departThirdHtml += "<option value = '" + val.id + "'>" + val.name + "</option>";
                        })

                    } else {
                        departThirdHtml = "<option value = 'qita'>三级部门</option>";
                    }
                    $("#depart3").html(departThirdHtml);
                }
            })
        }
    })

    var sel = $.Params.create();
    sel.addParams("page", 0);
    sel.addParams("rows", rows);
    getData();

    //查询
    $("#searchBtn").on("click", function () {
        var pTitle = $("#pTitle").val().trim();
        var depart1 = $("#depart1").val();
        var depart2 = $("#depart2").val();
        var depart3 = $("#depart3").val();
        if (pTitle != "") {
            sel.addParams("pTitle", pTitle);
        } else {
            sel.addParams("pTitle", undefined);
        }
        if (depart1 != "qita") {
            if (depart2 != "qita") {
                if (depart3 != "qita") {
                    sel.addParams("filed4", depart3);
                } else {
                    sel.addParams("filed4", depart2);
                }
            } else {
                sel.addParams("filed4", depart1);
            }
        } else {
            sel.addParams("filed4", undefined);
        }
        getData();
    })

    //导出
    $("#importBtn").on("click", function () {
        var pTitle = $("#pTitle").val().trim();
        var depart1 = $("#depart1").val();
        var depart2 = $("#depart2").val();
        var depart3 = $("#depart3").val();
        var data = "";
        if (pTitle != "") {
            data += "&pTitle=" + pTitle;
        }
        if (depart1 != "qita") {
            if (depart2 != "qita") {
                if (depart3 != "qita") {
                    data += "&filed4=" + depart3;
                } else {
                    data += "&filed4=" + depart2;
                }
            } else {
                data += "&filed4=" + depart1;
            }
        }
        window.location.href = getIP() + "memberRespresent/getmemberRespresentExcel?1=1" + data;
    })

    //判断checkbox状态
    $("#selAll").on("click", function () {
        if (!$(this).attr("checked")) {
            $("input[type=checkbox]").attr("checked", "checked")
        } else {
            $("input[type=checkbox]").removeAttr("checked");
        }
    })



    //删除事件
    $('#delBtn').on("click", function () {
        var params = getCheckData();
        if (params != "") {
            if (confirm("确定要删除吗！")) {
                var del = $.Params.create();
                del.addParams("params", params)
                del.ajax(getIP() + "memberRespresent/deleteMemberRespresent", function (data) {
                    if (data.success) {
                        alert("删除成功！");
                        getData();
                    }
                })
            }
        } else {
            alert("请至少选中一项");
        }
    });


    //获取数据
    function getData() {
        getPageData(sel, getIP() + "memberRespresent/getMemberRespresentPage", function (data) {
            if (data.success) {
                var html = "";
                $.each(data.result, function (i, val) {
                    var sex = val.sex;
                    if (val.sex == 1) {
                        sex = "男";
                    } else if (val.sex == 2) {
                        sex = "女";
                    } else {
                        sex = "";
                    }
                    var depart = "";
                    if (val.Department != null) {
                        depart = val.Department.name;
                    }


                    html += "<tr role='row'' class='even'>" +
                        "<td><input eid='" + val.userId + "' onclick='getCheck(this)' type='checkbox' class='classOption'></td>" +
                        "<td class='sorting_1'>" + val.name + "</td>" +
                        "<td>" + val.tel + "</td>" +
                        "<td>" + val.email + "</td>" +
                        "<td>" + sex + "</td>" +
                        "<td>" + depart + "</td>" +
                        "<td>" + val.post + "</td>" +
                        "<td><a href='memberDoctorList.html?filed8=" + val.userId + "'>" + val.members.length + "</a></td>" +
                        "<td>" + val.create_time + "</td>" +
                        //"<td><button type='button' class='btn btn-primary'  onclick='updateInfo(\""+val.userId+"\")'>修改</button></td>" +
                        "</tr>";
                });
                $("#listContainer").html(html);
            }
        })
    }

});

//function updateInfo(eid){
//    window.location.href="memberRepresentUpdate.html?eid="+eid;
//}


